package com.tourex.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tourex.App;
import com.tourex.BaseActivity;
import com.tourex.BaseFragment;
import com.tourex.C;
import com.tourex.C.bean.TourProjectInfo;
import com.tourex.R;
import com.tourex.ui.GeneralListActivity;
import com.tourex.ui.SecondActivity;
import com.tourex.ui.WapActivity;

public class Second1Fragment extends BaseFragment {

	private SecondActivity holder;

	// private ListView listView;
	private GridView gridView;
	private TourProjectInfoAdapter tourProjectInfoAdapter = new TourProjectInfoAdapter();
	private List<TourProjectInfo> tourProjectInfoList = new ArrayList<TourProjectInfo>();

	private Button btnMore;
	private View layMore;

	private String type;

	public static Second1Fragment newInstance(String type) {
		Second1Fragment second1Fragment = new Second1Fragment();
		second1Fragment.type = type;
		return second1Fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof SecondActivity) {
			holder = (SecondActivity) activity;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_second1_double, null);
		layMore = view.findViewById(R.id.layout_more);
		btnMore = (Button) view.findViewById(R.id.btn_more);
		if (C.convention.type_cz.equals(type)) {
			layMore.setVisibility(View.GONE);
			btnMore.setOnClickListener(null);
		} else {
			layMore.setVisibility(View.VISIBLE);
			btnMore.setOnClickListener(onMoreClickListener);
		}

		gridView = (GridView) view.findViewById(R.id.gridview);
		gridView.setAdapter(tourProjectInfoAdapter);
		gridView.setOnItemClickListener(onItemClickListener);
		gridView.post(new Runnable() {
			@Override
			public void run() {
				holder.requestApi02(type, App.i().mli.cityCode);
			}
		});
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	OnItemClickListener onItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(android.widget.AdapterView<?> arg0, View arg1, int position, long arg3) {
			TourProjectInfo ti = tourProjectInfoList.get(position);
			if (TextUtils.isEmpty(ti.url)) {
				holder.showToast(R.string.null_url);
			} else {
				WapActivity.actionStart(holder, ti.url, type, ti.phone, ti.jd, ti.wd);
			}
		}
	};

	OnClickListener onMoreClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			GeneralListActivity.actionStart(holder, type, null);
		}
	};

	public void setData(List<TourProjectInfo> data) {
		tourProjectInfoList = data;
		tourProjectInfoAdapter.notifyDataSetChanged();
	}

	class TourProjectInfoAdapter extends BaseAdapter {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final TourProjectInfo tpi = (TourProjectInfo) getItem(position);
			View view;
			final ViewHolder viewHolder;
			if (convertView == null) {
				view = LayoutInflater.from(holder).inflate(R.layout.list_content_second1, null);
				viewHolder = new ViewHolder();
				viewHolder.imageView = (ImageView) view.findViewById(R.id.imageview);
				viewHolder.textView = (TextView) view.findViewById(R.id.textview);

				view.setTag(viewHolder);
			} else {
				view = convertView;
				viewHolder = (ViewHolder) view.getTag();
			}

			App.i().imageLoader.get(tpi.img, BaseActivity.getDefaultImageListener(viewHolder.imageView));

			viewHolder.textView.setText(tpi.title);

			return view;
		}

		class ViewHolder {
			TextView textView;
			ImageView imageView;
		}

		@Override
		public int getCount() {
			return tourProjectInfoList.size();
		}

		@Override
		public Object getItem(int position) {
			return tourProjectInfoList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}
}